﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRoomInspection.Model;
using YJKRoomInspection.ShowDataForm.ModellessFrame;
using YJKRoomInspection.SpecialUse;
using YJKRevitBase.WinForm.BackGroundProgressBar;

namespace YJKRoomInspection.ProgramArchitecture
{
	public class PublicYJKRoomInspection
	{
		public ExternalCommandData UseCommand
		{
			get
			{
				return this.m_UseCommand;
			}
			private set
			{
				this.m_UseCommand = value;
			}
		}

		public UIApplication UiApp
		{
			get
			{
				return this.m_UiApp;
			}
			private set
			{
				this.m_UiApp = value;
			}
		}

        public Autodesk.Revit.ApplicationServices.Application App
		{
			get
			{
				return this.m_app;
			}
			private set
			{
				this.m_app = value;
			}
		}

		public UIDocument UiDoc
		{
			get
			{
				return this.m_UiDoc;
			}
			private set
			{
				this.m_UiDoc = value;
			}
		}

		public Document Doc
		{
			get
			{
				return this.m_doc;
			}
			private set
			{
				this.m_doc = value;
			}
		}

		public List<Level> LstLevel
		{
			get
			{
				return this.m_lstLevel;
			}
			private set
			{
				this.m_lstLevel = value;
			}
		}

		public IWin32Window UseRevitHandle
		{
			get
			{
				return this.m_UseRevitHandle;
			}
			private set
			{
				this.m_UseRevitHandle = value;
			}
		}

		public bool IfManualAbosrt
		{
			get
			{
				return this.m_bIfManualAbosrt;
			}
			private set
			{
				this.m_bIfManualAbosrt = value;
			}
		}

		public static List<int> LstFrontRoomIds
		{
			get
			{
				return PublicYJKRoomInspection.m_lstFrontRoomIds;
			}
			private set
			{
				PublicYJKRoomInspection.m_lstFrontRoomIds = value;
			}
		}

		public PublicYJKRoomInspection(ExternalCommandData commandData, List<Level> lstLevel, IWin32Window inputHandle)
		{
			this.UseCommand = commandData;
			this.UiApp = commandData.Application;
			this.App = this.UiApp.Application;
			this.UiDoc = this.UiApp.ActiveUIDocument;
			this.Doc = this.UiDoc.Document;
			this.LstLevel = lstLevel;
			this.UseRevitHandle = inputHandle;
		}

		public void DoCheck()
		{
			PublicYJKRoomInspection.LstFrontRoomIds = new List<int>();
			ProgressBarManger progressBarManger = new ProgressBarManger(new BackWorkDelegate(this.BackGroundWork));
			progressBarManger.ShowFormAndDoWork(this.UseRevitHandle);
			if (progressBarManger.IfManualAbsort)
			{
				this.IfManualAbosrt = true;
				return;
			}
			new ViewLayerManger().DoViewLayer(this.UseCommand, this.UseRevitHandle, this.UiDoc, this.m_lstAllResult);
		}

		private void BackGroundWork()
		{
			this.m_lstAllFindFrontRooms = this.m_frontRoomFinder.FindPackers(this.Doc, new object[]
			{
				this.LstLevel
			});
			this.m_lstAllFindFrontRooms = this.m_frontRoomChecker.CheckPackers(this.m_lstAllFindFrontRooms, Array.Empty<object>());
			foreach (Level level in this.LstLevel)
			{
				this.m_lstAllResult.Add(new AdapteRoomResulePacker
				{
					UseLevel = level,
					RegionZVal = level.Elevation
				});
			}
			foreach (FrontRoomPacker frontRoomPacker in this.m_lstAllFindFrontRooms)
			{
				this.m_lstAllResult.Add(new AdapteRoomResulePacker
				{
					FrontRoomPacker = frontRoomPacker,
					UseLevel = frontRoomPacker.ThisRoom.Level,
					RegionZVal = frontRoomPacker.ThisRoom.Level.Elevation,
					AddRoomType = frontRoomPacker.thisAreaOrSide.FrontRoomType
				});
			}
		}

		private ExternalCommandData m_UseCommand;

		private UIApplication m_UiApp;

        private Autodesk.Revit.ApplicationServices.Application m_app;

		private UIDocument m_UiDoc;

		private Document m_doc;

		private List<Level> m_lstLevel;

		private IWin32Window m_UseRevitHandle;

		private static List<int> m_lstFrontRoomIds = new List<int>();

		private bool m_bIfManualAbosrt;

		private YJKRoomInspection_CmdFrontRoomFind m_frontRoomChecker = new YJKRoomInspection_CmdFrontRoomFind();

		private IFrontRoomFind<FrontRoomPacker> m_frontRoomFinder = new FrontRoomFind_CmdFrontRoomFind();

		private List<FrontRoomPacker> m_lstAllFindFrontRooms = new List<FrontRoomPacker>();

		private List<AdapteRoomResulePacker> m_lstAllResult = new List<AdapteRoomResulePacker>();
	}
}
